package com.baseframe.topic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.mybatis.utils.BaseMetaObjectUtil;
import com.baseframe.tool.result.R;
import com.baseframe.topic.entity.SnsTestTopicItem;
import com.baseframe.topic.entity.SnsTestTopicItemOption;
import com.baseframe.topic.entity.request.SnsTestTopicItemRequest;
import com.baseframe.topic.entity.vo.SnsTestTopicItemVO;
import com.baseframe.topic.service.SnsTestTopicItemOptionService;
import com.baseframe.topic.service.SnsTestTopicItemService;
import com.baseframe.topic.mapper.SnsTestTopicItemMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**

* @description 针对表【sns_test_topic_item(测验题目表)】的数据库操作Service实现
* @createDate 2023-06-29 23:12:25
*/
@Service
@AllArgsConstructor
public class SnsTestTopicItemServiceImpl extends ServiceImpl<SnsTestTopicItemMapper, SnsTestTopicItem>
    implements SnsTestTopicItemService{

    private SnsTestTopicItemOptionService snsTestTopicItemOptionService;

    @Override
    public Integer totalScore(Long topicId) {
        return getBaseMapper().totalScore(topicId);
    }

    @Override
    public List<SnsTestTopicItemVO> getTopItems(SnsTestTopicItemRequest request) {




        return this.getBaseMapper().getTopicItems(request.getTopicId());
    }

    @Override
    @SuppressWarnings("all")
    public R<Object> saveOrUpdateTopicItemAndItemOption(SnsTestTopicItemRequest request) {
        SnsTestTopicItem snsTestTopicItem = new SnsTestTopicItem();
        BeanUtils.copyProperties(request,snsTestTopicItem, BaseMetaObjectUtil.getUpdateFillPropertiesName(SnsTestTopicItem.class));

        //先保存题目 这里是自调用,如果这里后面加事务的话，需要将自调用改掉，否则的话，可能回导致事务失效
        boolean itemSaveOrUpdate = saveOrUpdate(snsTestTopicItem);

        if (!itemSaveOrUpdate){
            return R.fail("保存失败~");
        }

        //获取该题目提交的选项
        List<SnsTestTopicItemOption> optionList = request.getOptionList();

        optionList.forEach(e->{
            e.setTopicItemId(snsTestTopicItem.getId());
        });

        //批量存储选项
        boolean optionSaveOrUpdate = snsTestTopicItemOptionService.saveOrUpdateBatch(optionList);

        if (!optionSaveOrUpdate){
            return R.fail("保存失败~");
        }
        //将正确答案填写回目标题目
        SnsTestTopicItemOption itemOptionOne = snsTestTopicItemOptionService.getOne(new LambdaQueryWrapper<SnsTestTopicItemOption>().eq(SnsTestTopicItemOption::getTopicItemId, snsTestTopicItem.getId()).eq(SnsTestTopicItemOption::getName, request.getCorrectAnswerItemName()));
        this.update(new LambdaUpdateWrapper<SnsTestTopicItem>().eq(SnsTestTopicItem::getId,snsTestTopicItem.getId()).set(SnsTestTopicItem::getCorrectAnswer,itemOptionOne.getId()));


        return R.success();
    }
}




